﻿using System;
using GrainManage.Base;
using GrainManage.Entities.Filter;
using GrainManage.Entities.Price;
using GrainManage.Tools;
using GrainManage.Api;
using GrainManage.Proxy;

namespace GrainManage.Pages
{
    /// <summary>
    /// Interaction logic for GrainPage.xaml
    /// </summary>
    public partial class PricePage : BasePage
    {
        public PricePage()
        {
            InitializeComponent();

            RegisterService();

            BindQueryEvent();

            BindDropDownEvent();

            BindCheckedEvent();

            myPager.StartService(PriceApi.GetRecentPrice);
        }

        private string CurrentService = PriceApi.GetRecentPrice;

        private void RegisterService()
        {
            myPager.RegisterService((pageIndex, pageSize) =>
            {
                var filter = GetPriceFilter();
                TryBeginInvoke(() =>
                {
                    InputGetRecentPrice input = new InputGetRecentPrice
                    {
                        Authentication = App.Authentication,
                        PriceFilter = filter,
                        PageIndex = pageIndex,
                        PageSize = pageSize
                    };
                    return Creator.PriceProxy.GetRecentPrice(input);

                }, (result) =>
                {
                    UI.ShowContent(listview, result.Prices);
                    myPager.TotalRowCount = result.Total;
                });
            }, PriceApi.GetRecentPrice);

            myPager.RegisterService((pageIndex, pageSize) =>
            {
                var filter = GetPriceFilter();
                TryBeginInvoke(() =>
                {
                    InputGetPrice input = new InputGetPrice
                    {
                        Authentication = App.Authentication,
                        PriceFilter = filter,
                        PageIndex = pageIndex,
                        PageSize = pageSize
                    };
                    return Creator.PriceProxy.GetPrice(input);
                }, (result) =>
                {
                    UI.ShowContent(listview, result.Prices);
                    myPager.TotalRowCount = result.Total;
                });
            }, PriceApi.GetPrice);
        }

        private void BindDropDownEvent()
        {
            var eh = new GrainManage.Tools.EventHelper();
            eh.AddDropDownOpenedHandler(cbPriceType, () =>
            {
                TryBeginInvoke(() =>
                {
                    return Creator.PriceProxy.GetPriceType(PageSettings.PriceTypeCount);
                }, (result) =>
                {
                    UI.ShowContent(cbPriceType, result);
                });
            });

            eh.AddDropDownOpenedHandler(cbGrain, () =>
            {
                TryBeginInvoke(() =>
                {
                    return Creator.PriceProxy.GetGrainType(PageSettings.GrainTypeCount);
                }, (result) =>
                {
                    UI.ShowContent(cbGrain, result);
                });
            });
        }

        private void BindQueryEvent()
        {
            var eh = new GrainManage.Tools.EventHelper();
            //*****************************************按键与双击事件*******************************************
            Action cbQueryAction = () => myPager.StartService(CurrentService);
            eh.AddQueryEventHandler(cbPriceType, cbQueryAction);
            eh.AddQueryEventHandler(cbGrain, cbQueryAction);
            //btnQuery.Click += (sender, e) => cbQueryAction();
        }

        private void BindCheckedEvent()
        {
            var eh = new GrainManage.Tools.EventHelper();
            eh.AddCheckedEventHandler(rbtnRecent, () => { CurrentService = PriceApi.GetRecentPrice; myPager.StartService(CurrentService); });
            eh.AddCheckedEventHandler(rbHistory, () => { CurrentService = PriceApi.GetPrice; myPager.StartService(CurrentService); });
        }

        private PriceFilter GetPriceFilter()
        {
            var filter = new PriceFilter();
            filter.PriceType = cbPriceType.Text.Trim(' ');
            filter.Grain = cbGrain.Text.Trim(' ');
            filter.StartTime = Convertor.ToDateTime(timeStart.Text.Trim(' '));
            filter.EndTime = Convertor.ToDateTime(timeEnd.Text.Trim(' '));
            return filter;
        }
    }
}
